package cn.gmcx.lazy.izze.service.impl;

import cn.gmcx.lazy.izze.pojo.Comments;
import cn.gmcx.lazy.izze.mapper.CommentsMapper;
import cn.gmcx.lazy.izze.pojo.CommentsVo;
import cn.gmcx.lazy.izze.service.FileService;
import cn.gmcx.lazy.izze.service.ICommentsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 评论表 用户对视频的评论 服务实现类
 * </p>
 *
 * @author GMCX
 * @since 2024-06-14
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements ICommentsService {

    @Autowired
    CommentsMapper commentsMapper;
    @Autowired
    FileService fileService;
    @Override
    public List<CommentsVo> selectByVideoId(Integer videoId) {

       List<CommentsVo> parentCommentList = commentsMapper.selectByVideoId(videoId);
       parentCommentList.forEach(item->{
           QueryWrapper<Comments> commentsQueryWrapper = new QueryWrapper<>();
           commentsQueryWrapper.eq("parent_id",item.getId());
           Long l = commentsMapper.selectCount(commentsQueryWrapper);
           item.setSubCommentCount(l);
       });
       return parentCommentList;
    }

    @Override
    public Integer addComments(Comments comments, MultipartFile file) {
        if (!Objects.isNull(file)){
            String imgUrlComment = fileService.getImg_url_comment(file);
            comments.setImgUrl(imgUrlComment);
        }
        return commentsMapper.addComments(comments);
    }

    @Override
    public List<CommentsVo> selectByParentId(Integer parentId) {
        List<CommentsVo> commentsVos = commentsMapper.selectByParentId(parentId);
        return commentsVos;
    }


}
